﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.GoodsModels;
using IceCS.GoodsSale.Models.UserModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmSaleDataCount : Form
    {
        public FrmSaleDataCount()
        {
            InitializeComponent();
        }

        SaleManager saleManager = new SaleManager();
        string selectSeller = "";
        string selectDate = "";

        decimal TotalPriceCount()
        {
            decimal totalPrice = 0;
            for (int i = 0; i <= dgrdvSale.Rows.Count - 1; i++)
            {
                totalPrice += decimal.Parse(dgrdvSale.Rows[i].Cells[6].Value.ToString());
            }
            return totalPrice;
        }

        decimal EarningsPriceCount()
        {
            decimal earnings = 0;
            for (int i = 0; i <= dgrdvSale.Rows.Count - 1; i++)
            {
                earnings += decimal.Parse(dgrdvSale.Rows[i].Cells[7].Value.ToString());
            }
            return earnings;
        }

        void SetHeaderText()
        {
            dgrdvSale.Columns[0].HeaderText = "单号";
            dgrdvSale.Columns[0].Width = 56;
            dgrdvSale.Columns[1].HeaderText = "商品号";
            dgrdvSale.Columns[1].Width = 94;
            dgrdvSale.Columns[2].HeaderText = "商品名";
            dgrdvSale.Columns[2].Width = 120;
            dgrdvSale.Columns[3].HeaderText = "进价";
            dgrdvSale.Columns[3].Width = 60;
            dgrdvSale.Columns[4].HeaderText = "售价";
            dgrdvSale.Columns[4].Width = 60;
            dgrdvSale.Columns[5].HeaderText = "数量";
            dgrdvSale.Columns[5].Width = 60;
            dgrdvSale.Columns[6].HeaderText = "总价";
            dgrdvSale.Columns[6].Width = 60;
            dgrdvSale.Columns[7].HeaderText = "利润";
            dgrdvSale.Columns[7].Width = 60;
            dgrdvSale.Columns[8].HeaderText = "销售日期";
            dgrdvSale.Columns[8].Width = 100;
            dgrdvSale.Columns[9].HeaderText = "销售员";
            dgrdvSale.Columns[9].Width = 80;
        }

        private void FrmSaleDataCount_Load(object sender, EventArgs e)
        {
            try
            {
                cmbSelectSeller.Items.Clear();
                cmbSelectSeller.Items.Add("全部");
                UserManager userManager = new UserManager();
                List<User> list = userManager.GetUserNames();
                foreach (User u in list)
                {
                    cmbSelectSeller.Items.Add(u.UserName);
                }
                cmbSelectSeller.SelectedIndex = 0;
                cmbSelectDate.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dgrdvSale_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgrdvSale.CurrentCell == null)
            {
                return;
            }
            int n = dgrdvSale.CurrentCell.RowIndex;
            dgrdvSale.Rows[n].Selected = true;
        }

        private void cmbSelectSeller_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectSeller = cmbSelectSeller.SelectedItem.ToString();
            cmbSelectDate.Focus();
        }

        private void cmbSelectDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbSelectDate.SelectedIndex == 0)
            {
                cmbYear.Enabled = false;
                cmbYear.SelectedIndex = -1;
                cmbMonth.Enabled = false;
                cmbMonth.SelectedIndex = -1;
                cmbDate.Enabled = false;
                cmbDate.SelectedIndex = -1;
            }
            if (cmbSelectDate.SelectedIndex == 1)
            {
                cmbYear.Enabled = true;
                cmbYear.SelectedIndex = 0;
                cmbMonth.Enabled = false;
                cmbMonth.SelectedIndex = -1;
                cmbDate.Enabled = false;
                cmbDate.SelectedIndex = -1;
            }
            else if (cmbSelectDate.SelectedIndex == 2)
            {
                cmbYear.Enabled = true;
                cmbYear.SelectedIndex = 0;
                cmbMonth.Enabled = true;
                cmbMonth.SelectedIndex = 0;
                cmbDate.Enabled = false;
                cmbDate.SelectedIndex = -1;
            }
            else if (cmbSelectDate.SelectedIndex == 3)
            {
                cmbYear.Enabled = true;
                cmbYear.SelectedIndex = 0;
                cmbMonth.Enabled = true;
                cmbMonth.SelectedIndex = 0;
                cmbDate.Enabled = true;
                cmbDate.Items.Clear();
                for (int i = 1; i <= 31; i++)
                {
                    cmbDate.Items.Add(i.ToString());
                }
                cmbDate.SelectedIndex = 0;
            }
            cmbYear.Focus();
        }

        private void cmbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!cmbYear.Enabled)
            {
                return;
            }
            if (!cmbMonth.Enabled)
            {
                selectDate = cmbYear.SelectedItem.ToString() + "年";
            }
            else if (!cmbDate.Enabled)
            {
                selectDate = cmbYear.SelectedItem.ToString() + "年" + cmbMonth.SelectedItem.ToString() + "月";
            }
            else
            {
                selectDate = cmbYear.SelectedItem.ToString() + "年" + cmbMonth.SelectedItem.ToString() + "月" + cmbDate.SelectedItem.ToString() + "日";
            }
            if (cmbMonth.Enabled && cmbDate.Enabled)
            {
                int month = int.Parse(cmbMonth.SelectedItem.ToString());
                if (month == 2)
                {
                    int year = int.Parse(cmbYear.SelectedItem.ToString());
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    {
                        cmbDate.Items.Clear();
                        for (int i = 1; i <= 29; i++)
                        {
                            cmbDate.Items.Add(i.ToString());
                        }
                        cmbDate.SelectedIndex = 0;
                    }
                    else
                    {
                        cmbDate.Items.Clear();
                        for (int i = 1; i <= 28; i++)
                        {
                            cmbDate.Items.Add(i.ToString());
                        }
                        cmbDate.SelectedIndex = 0;
                    }
                }
                else if (month == 4 || month == 6 || month == 9 || month == 11)
                {
                    cmbDate.Items.Clear();
                    for (int i = 1; i <= 30; i++)
                    {
                        cmbDate.Items.Add(i.ToString());
                    }
                    cmbDate.SelectedIndex = 0;
                }
                else
                {
                    cmbDate.Items.Clear();
                    for (int i = 1; i <= 31; i++)
                    {
                        cmbDate.Items.Add(i.ToString());
                    }
                    cmbDate.SelectedIndex = 0;
                }
            }
        }

        private void cmbMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!cmbMonth.Enabled)
            {
                return;
            }
            if (!cmbDate.Enabled)
            {
                selectDate = cmbYear.SelectedItem.ToString() + "年" + cmbMonth.SelectedItem.ToString() + "月";
            }
            else
            {
                selectDate = cmbYear.SelectedItem.ToString() + "年" + cmbMonth.SelectedItem.ToString() + "月" + cmbDate.SelectedItem.ToString() + "日";
                int month = int.Parse(cmbMonth.SelectedItem.ToString());
                if (month == 2)
                {
                    int year = int.Parse(cmbYear.SelectedItem.ToString());
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                    {
                        cmbDate.Items.Clear();
                        for (int i = 1; i <= 29; i++)
                        {
                            cmbDate.Items.Add(i.ToString());
                        }
                        cmbDate.SelectedIndex = 0;
                    }
                    else
                    {
                        cmbDate.Items.Clear();
                        for (int i = 1; i <= 28; i++)
                        {
                            cmbDate.Items.Add(i.ToString());
                        }
                        cmbDate.SelectedIndex = 0;
                    }
                }
                else if (month == 4 || month == 6 || month == 9 || month == 11)
                {
                    cmbDate.Items.Clear();
                    for (int i = 1; i <= 30; i++)
                    {
                        cmbDate.Items.Add(i.ToString());
                    }
                    cmbDate.SelectedIndex = 0;
                }
                else
                {
                    cmbDate.Items.Clear();
                    for (int i = 1; i <= 31; i++)
                    {
                        cmbDate.Items.Add(i.ToString());
                    }
                    cmbDate.SelectedIndex = 0;
                }
            }
        }

        private void cmbDate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDate.Enabled)
            {
                selectDate = cmbYear.SelectedItem.ToString() + "年" + cmbMonth.SelectedItem.ToString() + "月" + cmbDate.SelectedItem.ToString() + "日";
            }
        }

        private void btnCount_Click(object sender, EventArgs e)
        {
            try
            {
                if (cmbSelectSeller.SelectedIndex == 0 && cmbSelectDate.SelectedIndex == 0)
                {
                    List<Sale> list = saleManager.GetSales();
                    dgrdvSale.DataSource = list;
                    SetHeaderText();
                    txtTotal.Text = TotalPriceCount().ToString();
                    txtEarnings.Text = EarningsPriceCount().ToString();
                    return;
                }
                if (cmbSelectSeller.SelectedIndex != 0 && cmbSelectDate.SelectedIndex == 0)
                {
                    List<Sale> list = saleManager.GetSellerSales(selectSeller);
                    dgrdvSale.DataSource = list;
                    SetHeaderText();
                    txtTotal.Text = TotalPriceCount().ToString();
                    txtEarnings.Text = EarningsPriceCount().ToString();
                    return;
                }
                if (cmbSelectSeller.SelectedIndex == 0 && cmbSelectDate.SelectedIndex != 0)
                {
                    List<Sale> list = saleManager.GetDateSales(selectDate);
                    dgrdvSale.DataSource = list;
                    SetHeaderText();
                    txtTotal.Text = TotalPriceCount().ToString();
                    txtEarnings.Text = EarningsPriceCount().ToString();
                    return;
                }
                if (cmbSelectSeller.SelectedIndex != 0 && cmbSelectDate.SelectedIndex != 0)
                {
                    List<Sale> list = saleManager.GetSellerAndDateSales(selectSeller, selectDate);
                    dgrdvSale.DataSource = list;
                    SetHeaderText();
                    txtTotal.Text = TotalPriceCount().ToString();
                    txtEarnings.Text = EarningsPriceCount().ToString();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
